<?php

class AuthHelper {
	
	private $sessionHelper;
	private $redirectHelper;
	
	private $tableName;
	
	private $userColumn;
	private $passColumn;
	private $user;
	private $pass;
	
	private $loginController;
	private $loginAction;
	private $logoutController;
	private $logoutAction;
	
	private $extras;
	
	public function __construct() {
		$this->sessionHelper = new SessionHelper();
		$this->redirectHelper = new RedirectHelper();
		$this->extras = array();
		return $this;
	}
	
	public function setTableName($tableName) {
		$this->tableName = $tableName;
		return $this;
	}

	public function setUserColumn($userColumn) {
		$this->userColumn = $userColumn;
		return $this;
	}

	public function setPassColumn($passColumn) {
		$this->passColumn = $passColumn;
		return $this;
	}

	public function setUser($user) {
		$this->user = $user;
		return $this;
	}

	public function setPass($pass) {
		$this->pass = $pass;
		return $this;
	}

	public function setLoginControllerAction($loginController,$loginAction) {
		$this->loginController = $loginController;
		$this->loginAction = $loginAction;
		return $this;
	}

	public function setLogoutControllerAction($logoutController,$logoutAction) {
		$this->logoutController = $logoutController;
		$this->logoutAction = $logoutAction;
		return $this;
	}

	public function login() {
		$model = new Model($this->tableName);
		$data = array( "{$this->userColumn}" => "{$this->user}", "{$this->passColumn}" => "{$this->pass}");
		$arr = $model->find($data);
		if( count($arr) == 0 ){
			$this->redirectHelper
					->setParameter("error", "erroLogin")
					->goToControllerAction($this->logoutController, $this->logoutAction);
			exit();
		}else if( count($arr) == 1 ){
			$this->sessionHelper
				->createSession("userAuth", true)
				->createSession("userData", $arr[0])
				->createSession("userExtras", array());
		}else {
			$this->redirectHelper
					->setParameter("error", "erroLogin2")
					->goToControllerAction($this->logoutController, $this->logoutAction);
			exit();
		}
		$this->redirectHelper->goToControllerAction($this->loginController, $this->loginAction);
		return $this;
	}
	
	public function logout() {
		$this->sessionHelper
			->deleteSession("userAuth")
			->deleteSession("userData")
			->deleteSession("userExtras");
		$this->redirectHelper->goToControllerAction($this->logoutController, $this->logoutAction);
		return $this;
	}
	
	public function checkLogin() {
		if ( !$this->sessionHelper->checkSession("userAuth") ){
			if( $this->redirectHelper->getCurrentController() != $this->logoutController || $this->redirectHelper->getCurrentAction() != $this->logoutAction )
				$this->redirectHelper->goToControllerAction($this->logoutController, $this->logoutAction);
		}
		// Adaptação para utilizar este método sem sessão
		else{
			if( $this->loginController != null && $this->loginAction != null )
				$this->redirectHelper->goToControllerAction($this->loginController, $this->loginAction);
		}
	}
	
	public function testLogin() {
		return ($this->sessionHelper->checkSession("userAuth"));
	}
	
	public function getUserData($param) {
		if( $this->sessionHelper->checkSession("userAuth") ){
			$s = $this->sessionHelper->selectSession("userData");
			if( array_key_exists($param, $s) )
				return $s[$param];
		}
		return null;
	}
	
	public function getAllUserData() {
		if( $this->sessionHelper->checkSession("userAuth") )
			return $this->sessionHelper->selectSession("userData");
		return null;
	}
	
	public function getUserExtra($param) {
		if( $this->sessionHelper->checkSession("userAuth") ){
			$this->extras = $this->sessionHelper->selectSession("userExtras");
			if( array_key_exists($param, $this->extras) )
				return $this->extras[ $param ];
		}
		return null;
	}
	
	public function cleanParam($param) {
		$response = $this->getUserExtra($param);
		if( $response != null ){
			unset( $this->extras[ $param ] );
			//$response = Collections::removePerKey($this->extras, $param);
			$this->sessionHelper->createSession("userExtras", $this->extras);
		}
		//print_r($this->extras);
	}
	
	public function getAllUserExtras() {
		if( $this->sessionHelper->checkSession("userAuth") )
			return $this->extras;
		return null;
	}
	
	public function setUserExtra($param, $value) {
		if( $this->sessionHelper->checkSession("userAuth") ){
			$this->extras = $this->sessionHelper->selectSession("userExtras");
			$this->extras[ $param ] = $value;
			$this->sessionHelper->createSession("userExtras", $this->extras);
			return $this;
		}
		return null;
	}
	
}

?>